import Mock from "mockjs";
import Result from "./Result";

const baseURL = "http:\/\/120\.79\.230\.8:8082";

/**
 * 验证码发送的请求拦截响应函数
 * @returns
 */
var authCode = "1234567";

function getAuthoCode() {
    authCode = "1234";
    return new Result(200, "业务正确执行", true);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-user\/user\/register\/obtain\/email\/authCode\/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
    /get/i,
    getAuthoCode
);

function changeAvatar(res) {
    console.log(JSON.parse(res.body));

    return new Result(
        200,
        "业务正确执行",
        "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
    );
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-user\/user\/info\/update\/avatar$/,
    /post/i,
    changeAvatar
);

/**
 * 处理注册接口
 * @param {Object} request 请求res信息
 */
function doRegister(request) {
    let body = JSON.parse(request.body);
    let code = 404;
    let message = "注册失败";
    let data = {};

    if (body.authCode === authCode) {
        code = 200;
        message = "注册成功";
        data = {
            account: body.account,
            userEmail: body.userEmail,
        };
    }

    return new Result(code, message, data);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-user\/user\/register$/,
    /post/i,
    doRegister
);

/**
 * 处理登录接口
 * @param {*} request
 */
function doLogin(request) {
    console.log(request);
    let tempUserInfo = {
        userId: 1,
        account: "lisi",
        password: "123456",
        userName: "小白",
        userAvatar: "https://img01.yzcdn.cn/vant/cat.jpeg",
        sex: "男",
        individualitySignature: "本人很懒，没有留下什么足迹",
        userEmail: "973851631@qq.com",
        userPhone: null,
    };

    let body = JSON.parse(request.body);
    let data = {};
    let code = 200;
    let message = "业务正确执行";
    if (
        body.account === tempUserInfo.account &&
        body.password === tempUserInfo.password
    ) {
        data = Object.assign({}, tempUserInfo, {
            password: "",
        });
    } else {
        // code = request
        code = 409;
        message = "账户不存在或者密码错误";
    }

    return new Result(code, message, data);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-user\/user\/login$/,
    /post/i,
    doLogin
);

/**
 * 获取商品列表信息
 */
function getGoodsList() {
    let data = [{
            mealType: {
                mealTypeId: 1,
                mealType: "肉趴类",
            },
            childType: [
                {
                    mealType: {
                        mealTypeId: 11,
                        mealType: "猪肉趴",
                    },
                    childType: [],
                    mealList: [{
                            mealId: 1,
                            mealTypeId: 3,
                            mealName: "菲力猪肉趴",
                            mealSales: 0,
                            mealInfo: "1天前",
                            mealPrice: 2000,
                            mealInventory: 772,
                            // mealType: {
                            //     mealTypeId: 3,
                            //     mealType: "蔬菜类",
                            // },
                            mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                        },
                        {
                            mealId: 2,
                            mealTypeId: 3,
                            mealName: "脸眼猪肉趴",
                            mealSales: 0,
                            mealInfo: "刚采摘",
                            mealPrice: 1000,
                            mealInventory: 152,
                            // mealType: {
                            //     mealTypeId: 3,
                            //     mealType: "蔬菜类",
                            // },
                            mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                        },
                    ],
                },
                {
                    mealType: {
                        mealTypeId: 12,
                        mealType: "牛肉趴",
                    },
                    childType: [],
                    mealList: [{
                            mealId: 3,
                            mealTypeId: 2,
                            mealName: "菲力牛肉趴",
                            mealSales: 0,
                            mealInfo: "菲力牛肉制作",
                            mealPrice: 10001,
                            mealInventory: 41,
                            // mealType: {
                            //     mealTypeId: 2,
                            //     mealType: "肉类",
                            // },
                            mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                        },
                        {
                            mealId: 4,
                            mealTypeId: 2,
                            mealName: "牛排",
                            mealSales: 0,
                            mealInfo: "鲜牛肉制作",
                            mealPrice: 1000,
                            mealInventory: 30,
                            // mealType: {
                            //     mealTypeId: 2,
                            //     mealType: "肉类",
                            // },
                            mealPictures: [
                                "https://img01.yzcdn.cn/vant/cat.jpeg",
                                "介绍图片",
                            ],
                        },
                    ],
                },
                {
                    mealType: {
                        mealTypeId: 13,
                        mealType: "家禽趴",
                    },
                    childType: [{
                            mealType: {
                                mealTypeId: 131,
                                mealType: "鸡肉趴",
                            },
                            childType: [],
                            mealList: [{
                                    mealId: 5,
                                    mealTypeId: 4,
                                    mealName: "菲力鸡肉趴",
                                    mealSales: 0,
                                    mealInfo: "1天前",
                                    mealPrice: 2000,
                                    mealInventory: 772,
                                    mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                                },
                                {
                                    mealId: 6,
                                    mealTypeId: 4,
                                    mealName: "脸眼鸡肉趴",
                                    mealSales: 0,
                                    mealInfo: "鸡肉",
                                    mealPrice: 1000,
                                    mealInventory: 152,
                                    // mealType: {
                                    //     mealTypeId: 3,
                                    //     mealType: "蔬菜类",
                                    // },
                                    mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                                },
                            ],
                        },
                        {
                            mealType: {
                                mealTypeId: 132,
                                mealType: "鸭肉趴",
                            },
                            childType: [],
                            mealList: [{
                                    mealId: 7,
                                    mealTypeId: 5,
                                    mealName: "菲力鸭肉趴",
                                    mealSales: 0,
                                    mealInfo: "鸭肉制作",
                                    mealPrice: 101,
                                    mealInventory: 41,
                                    // mealType: {
                                    //     mealTypeId: 2,
                                    //     mealType: "肉类",
                                    // },
                                    mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                                },
                                {
                                    mealId: 8,
                                    mealTypeId: 5,
                                    mealName: "鸭肉排",
                                    mealSales: 0,
                                    mealInfo: "鲜鸭肉制作",
                                    mealPrice: 100,
                                    mealInventory: 30,
                                    // mealType: {
                                    //     mealTypeId: 2,
                                    //     mealType: "肉类",
                                    // },
                                    mealPictures: [
                                        "https://img01.yzcdn.cn/vant/cat.jpeg",
                                        "介绍图片",
                                    ],
                                },
                            ],
                        },
                    ],
                    mealList: [],
                },
            ],
            mealList: [],
        },
        {
            mealType: {
                mealTypeId: 2,
                mealType: "焗饭类",
            },
            childType: [],
            mealList: [],
        },
        {
            mealType: {
                mealTypeId: 3,
                mealType: "意粉类",
            },
            childType: [],
            mealList: [],
        },
        {
            mealType: {
                mealTypeId: 4,
                mealType: "小食类",
            },
            childType: [],
            mealList: [],
        },
        {
            mealType: {
                mealTypeId: 5,
                mealType: "饮品类",
            },
            childType: [],
            mealList: [],
        },
        {
            mealType: {
                mealTypeId: 6,
                mealType: "蛋糕类",
            },
            childType: [],
            mealList: [],
        },
    ];

    return new Result(200, "成功", data);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-meal\/meal\/menu$/,
    /get/i,
    getGoodsList
);

function createOrder(res) {
    let body = JSON.parse(res.body);

    console.log(body);
    let data = {
        orderId: "7aa2e88d-501c-11ed-8cde-0242c0a89221",
        qrCode: "二维码的base64字符串",
    };

    return new Result(200, "业务正确执行", data);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-order\/order\/create$/,
    /post/i,
    createOrder
);

function flushCode(res) {
    let reg =
        /^http:\/\/120\.79\.230\.8:8082\/restaurant-order\/order\/flush\/qrCode\/(.*)$/;
    let result = res.url.match(reg);
    let orderId = result[1];

    let body = JSON.parse(res.body);

    let data = {
        orderId: "7aa2e88d-501c-11ed-8cde-0242c0a89221",
        qrCode: "二维码的base64字符串",
    };

    return new Result(200, "业务正确执行", data);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-order\/order\/flush\/qrCode\/.*$/,
    /get/i,
    flushCode
);

function checkSuccess(res) {
    let flag = true;
    // setTimeout(() => {
    //     flag = true;
    //     console.log("微信那边已经支付成功");
    // }, 10000);
    return new Result(200, "业务正确执行", flag);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-order\/order\/look\/payState\/.*$/,
    /get/i,
    checkSuccess
);

function getOrderList(res) {
    let body = JSON.parse(res.body);

    let data = {
        restaurant: {
            restaurantId: 1,
            restaurantName: "名苑西餐厅",
            restaurantPicture: [
                "https://img01.yzcdn.cn/vant/cat.jpeg",
                "餐饮厅图片2",
            ],
            restaurantPhone: "13719569516",
            restaurantState: "CLOSE",
        },
        briefOrder: [{
                orderId: "4639cd95-4f4a-11ed-bdc4-0242c0a89221",
                orderItems: [{
                        itemId: 31,
                        mealItem: {
                            mealId: 1,
                            mealName: "牛排",
                            mealType: {
                                mealTypeId: 2,
                                mealType: "肉类",
                            },
                            mealPictures: [
                                "https://img01.yzcdn.cn/vant/cat.jpeg",
                                "牛排图片2",
                            ],
                        },
                        purchasingMealPrice: 1,
                        purchaseNum: 1,
                    },
                    {
                        itemId: 32,
                        mealItem: {
                            mealId: 3,
                            mealName: "青菜",
                            mealType: {
                                mealTypeId: 3,
                                mealType: "蔬菜类",
                            },
                            mealPictures: ["https://img01.yzcdn.cn/vant/cat.jpeg"],
                        },
                        purchasingMealPrice: 1,
                        purchaseNum: 1,
                    },
                ],
                totalPrice: 2,
                orderState: "CANCEL_PAY",
                orderPurchaseTime: "2022-10-19 09:06:34",
            },
            {
                orderId: "1",
                orderItems: [{
                    itemId: 1,
                    mealItem: {
                        mealId: 1,
                        mealName: "牛排",
                        mealType: {
                            mealTypeId: 2,
                            mealType: "肉类",
                        },
                        mealPictures: [
                            "https://img01.yzcdn.cn/vant/cat.jpeg",
                            "牛排图片2",
                        ],
                    },
                    purchasingMealPrice: 36,
                    purchaseNum: 1,
                }, ],
                totalPrice: 36,
                orderState: "WAITING_PAY",
                orderPurchaseTime: "2022-10-17 09:39:23",
            },
        ],
    };

    return new Result(200, "业务正确执行", data);
}
Mock.mock(
    /^http:\/\/120\.79\.230\.8:8082\/restaurant-order\/order\/look\/briefOrder$/,
    /post/i,
    getOrderList
);